class Manage::StructuresController < Manage::MainController
  before_filter :find_structure, :only=>[:edit, :update, :destroy]
  
  # GET /manage/structures
  # GET /manage/structures.xml
  def index
    @structures = Structure.roots.find(:all)

    respond_to do |format|
      format.html # render index.html
      format.xml{ render :xml => @structures }
    end
  end
  
  # GET /manage/structures/new
  def new
    @structure = Structure.new
  end
  
  # POST /manage/structures
  # POST /manage/structures.xml
  def create
    @structure = Structure.new(params[:structure])
    @structure.slider = AttachmentFile.new
    
    respond_to do |format|
    	if @structure.valid? && @structure.save
    		
        flash[:notice] = I18n.t('flash.structure.created')
        
        format.html { redirect_to manage_structures_path }
        format.xml { head :ok }
      else
      	flash.now[:error] = I18n.t('flash.structure.create_error')
      	
        format.html { render :action => "new" }
        format.xml { render :xml => @structure.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /manage/structures/1
  # PUT /manage/structures/1.xml
  def update    
    respond_to do |format|
    	if @structure.update_attributes(params[:structure])
    		
        flash[:notice] = I18n.t('flash.structure.updated')
        
        format.html { redirect_to manage_structures_path }
        format.xml { head :ok }
      else
      	flash.now[:error] = I18n.t('flash.structure.update_error')
      	
        format.html { render :action => "edit" }
        format.xml { render :xml => @structure.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /manage/structures/1
  # DELETE /manage/structures/1.xml
  def destroy
    @structure.destroy
    flash[:notice] = I18n.t('flash.structure.destroy')
    
    respond_to do |format|
      format.html { redirect_to manage_structures_path }
      format.xml { head :ok }
    end
  end
  
  private
  
  def find_structure
    @structure = Structure.find(params[:id])
  end
end
